package cn.iocoder.yudao.module.erp.dal.mysql.product;

import cn.iocoder.yudao.framework.mybatis.core.mapper.BaseMapperX;
import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX;
import cn.iocoder.yudao.module.erp.controller.admin.product.vo.category.ErpCategorySettingPageReqVO;
import cn.iocoder.yudao.module.erp.dal.dataobject.product.ErpCategorySettingDO;
import org.apache.ibatis.annotations.Mapper;

import java.util.List;

/**
 * ERP 类别设定 Mapper
 *
 * @author 芋道源码
 */
@Mapper
public interface ErpCategorySettingMapper extends BaseMapperX<ErpCategorySettingDO> {

    default List<ErpCategorySettingDO> selectList(ErpCategorySettingPageReqVO reqVO) {
        return selectList(new LambdaQueryWrapperX<ErpCategorySettingDO>()
                .eqIfPresent(ErpCategorySettingDO::getParentId, reqVO.getParentId())
                .likeIfPresent(ErpCategorySettingDO::getName, reqVO.getName())
                .eqIfPresent(ErpCategorySettingDO::getType, reqVO.getType())
                .eqIfPresent(ErpCategorySettingDO::getStatus, reqVO.getStatus())
                .orderByAsc(ErpCategorySettingDO::getSort)
                .orderByDesc(ErpCategorySettingDO::getId));
    }

    /**
     * 查询所有主类别
     */
    default List<ErpCategorySettingDO> selectCategories() {
        return selectList(new LambdaQueryWrapperX<ErpCategorySettingDO>()
                .eq(ErpCategorySettingDO::getParentId, 0L)
                .eq(ErpCategorySettingDO::getType, "CATEGORY")
                .eq(ErpCategorySettingDO::getStatus, 1)
                .orderByAsc(ErpCategorySettingDO::getSort)
                .orderByDesc(ErpCategorySettingDO::getId));
    }

    /**
     * 根据父级ID查询子项目
     */
    default List<ErpCategorySettingDO> selectItemsByParentId(Long parentId) {
        return selectList(new LambdaQueryWrapperX<ErpCategorySettingDO>()
                .eq(ErpCategorySettingDO::getParentId, parentId)
                .eq(ErpCategorySettingDO::getType, "ITEM")
                .orderByAsc(ErpCategorySettingDO::getSort)
                .orderByDesc(ErpCategorySettingDO::getId));
    }

    /**
     * 根据父级ID和名称查询
     */
    default ErpCategorySettingDO selectByParentIdAndName(Long parentId, String name) {
        return selectOne(new LambdaQueryWrapperX<ErpCategorySettingDO>()
                .eq(ErpCategorySettingDO::getParentId, parentId)
                .eq(ErpCategorySettingDO::getName, name));
    }

    /**
     * 根据父级ID统计数量
     */
    default Long selectCountByParentId(Long parentId) {
        return selectCount(ErpCategorySettingDO::getParentId, parentId);
    }

}

